<?php

class userGalleryFormActions extends myFrontendAction {

  public function executeImageUpload(sfWebRequest $request) {
	sfProjectConfiguration::getActive()->loadHelpers(array('I18N'));
	
	$this->setLayout(false);
	$this->form = new ImageUploadForm();
	if ($files = $this->form->processForm($request)) {
	  $this->emailFiles($files, $request->getUriPrefix());
	  $this->getUser()->setFlash('attachmentNotice', __('piltide üleslaadimine õnnestus'));
	} else {
	  $this->getUser()->setFlash('attachmentNotice', __('piltide üleslaadimine ebaõnnestus, proovige uuesti. Võimalikud vead')
			  .':<br />'.implode('<br />',$this->form->getErrors()));
	}
	
	$struct = Doctrine::getTable('Structure')->find($request->getParameter('node_id'));
	$this->redirect($this->getComponent('linker', 'articleLinkBuilder', array('node' => $struct)));
  }
  
  protected function emailFiles($files, $host) {
	sfProjectConfiguration::getActive()->loadHelpers(array('Variable'));
	$links = '';
	foreach (array_keys($files) as $index => $id) {
	  $links .= $host."/backend.php/banner/$id/edit\n";
	}
	// Message to admins
	$message = Swift_Message::newInstance()
	  ->setFrom('katusemang@olly.ee')
	  ->setTo(variable('game_mail'))
	  ->setSubject('Katusemäng')
	  ->setBody("Tere!\n\nKatusemängus on uus osaleja.\n\nNimi: ".$this->form->getValue('name').
		  	    "\nPerekonnanimi: ".$this->form->getValue('lastname').
		  	    "\nTelefon: ".$this->form->getValue('phone').
		  	    "\nEmail: ".$this->form->getValue('email').
			    "\n\nOsaleja poolt saadetud pilte saate vaadata all lisandis.".
			    "\nKui soovite avalikustada neid veebilehe galeriis, pange linnuke 'is_active'".
			    " admin paneelis:\n".$links);
	foreach ($files as $file) {
	  $message->attach(Swift_Attachment::fromPath($file));
	}
	$this->getMailer()->send($message);
	
	// Message to user; for some reason can't reuse, so have to set attachments and etc again
	$message = Swift_Message::newInstance()
			->setFrom('katusemang@olly.ee')
			->setTo($this->form->getValue('email'))
		    ->setSubject('Katusemäng')
			->setBody("Ув. ".$this->form->getValue('name')." ".$this->form->getValue('lastname').
					 "!\n\nВы зарегестрированы в фотоконкурсе OLLY Katusemeistrid!".
					 "\nВами присланную фотографию смотрите ниже...".
					 "\n\nС Уважением,\nOLLY Katusemeistrid\nИНФО:тел.+372 56202188");
	foreach ($files as $file) {
	  $message->attach(Swift_Attachment::fromPath($file));
	}	
	$this->getMailer()->send($message);
  }
}